package com.itheima.test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
 * @author mewp
 * @date 2022/12/12 09:28
 **/
public class JacobDemo {
    public static void main(String[] args) {
        String source = "D:\\mewp\\资料\\Java报表数据可视化教程\\day04\\资料\\张三_合同.docx";
        String target = "D:\\mewp\\资料\\Java报表数据可视化教程\\day04\\资料\\张三_合同.pdf";
        ActiveXComponent app = null;
        try {
            //调用Window中的程序  需要将对应的jacob-1.19-x64.dll放到jdk\bin中
            app = new ActiveXComponent("Word.Application");
            //调用的时候不显示窗口
            app.setProperty("Visible", true);
            //获取所有打开的文档
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.call(docs, "Open", source).toDispatch();
            System.out.println("转换文档到PDF：" + target);
            //令存为，将文档保存为pdf，其中Word保存为PDF的格式宏的值是17
            Dispatch.call(doc, "SaveAs", target, 17);
            Dispatch.call(doc, "Close");
        } catch (Exception e) {
            System.out.println("Word转PDF出错：" + e.getMessage());
        } finally {
            if (app != null) {
                app.invoke("Quit", 0);
            }
        }

    }
}
